########## MACROS ###########################################################################
#############################################################################################

function(conan_message MESSAGE_OUTPUT)
    if(NOT CONAN_CMAKE_SILENT_OUTPUT)
        message(${ARGV${0}})
    endif()
endfunction()


macro(conan_find_apple_frameworks FRAMEWORKS_FOUND FRAMEWORKS FRAMEWORKS_DIRS)
    if(APPLE)
        foreach(_FRAMEWORK ${FRAMEWORKS})
            # https://cmake.org/pipermail/cmake-developers/2017-August/030199.html
            find_library(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND NAMES ${_FRAMEWORK} PATHS ${FRAMEWORKS_DIRS} CMAKE_FIND_ROOT_PATH_BOTH)
            if(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND)
                list(APPEND ${FRAMEWORKS_FOUND} ${CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND})
            else()
                message(FATAL_ERROR "Framework library ${_FRAMEWORK} not found in paths: ${FRAMEWORKS_DIRS}")
            endif()
        endforeach()
    endif()
endmacro()


function(conan_package_library_targets libraries package_libdir deps out_libraries out_libraries_target build_type package_name)
    unset(_CONAN_ACTUAL_TARGETS CACHE)
    unset(_CONAN_FOUND_SYSTEM_LIBS CACHE)
    foreach(_LIBRARY_NAME ${libraries})
        find_library(CONAN_FOUND_LIBRARY NAMES ${_LIBRARY_NAME} PATHS ${package_libdir}
                     NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
        if(CONAN_FOUND_LIBRARY)
            conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
            list(APPEND _out_libraries ${CONAN_FOUND_LIBRARY})
            if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
                # Create a micro-target for each lib/a found
                string(REGEX REPLACE "[^A-Za-z0-9.+_-]" "_" _LIBRARY_NAME ${_LIBRARY_NAME})
                set(_LIB_NAME CONAN_LIB::${package_name}_${_LIBRARY_NAME}${build_type})
                if(NOT TARGET ${_LIB_NAME})
                    # Create a micro-target for each lib/a found
                    add_library(${_LIB_NAME} UNKNOWN IMPORTED)
                    set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY})
                    set(_CONAN_ACTUAL_TARGETS ${_CONAN_ACTUAL_TARGETS} ${_LIB_NAME})
                else()
                    conan_message(STATUS "Skipping already existing target: ${_LIB_NAME}")
                endif()
                list(APPEND _out_libraries_target ${_LIB_NAME})
            endif()
            conan_message(STATUS "Found: ${CONAN_FOUND_LIBRARY}")
        else()
            conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
            list(APPEND _out_libraries_target ${_LIBRARY_NAME})
            list(APPEND _out_libraries ${_LIBRARY_NAME})
            set(_CONAN_FOUND_SYSTEM_LIBS "${_CONAN_FOUND_SYSTEM_LIBS};${_LIBRARY_NAME}")
        endif()
        unset(CONAN_FOUND_LIBRARY CACHE)
    endforeach()

    if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
        # Add all dependencies to all targets
        string(REPLACE " " ";" deps_list "${deps}")
        foreach(_CONAN_ACTUAL_TARGET ${_CONAN_ACTUAL_TARGETS})
            set_property(TARGET ${_CONAN_ACTUAL_TARGET} PROPERTY INTERFACE_LINK_LIBRARIES "${_CONAN_FOUND_SYSTEM_LIBS};${deps_list}")
        endforeach()
    endif()

    set(${out_libraries} ${_out_libraries} PARENT_SCOPE)
    set(${out_libraries_target} ${_out_libraries_target} PARENT_SCOPE)
endfunction()


########### FOUND PACKAGE ###################################################################
#############################################################################################

include(FindPackageHandleStandardArgs)

conan_message(STATUS "Conan: Using autogenerated FindRdKafka.cmake")
set(RdKafka_FOUND 1)
set(RdKafka_VERSION "1.9.1")

find_package_handle_standard_args(RdKafka REQUIRED_VARS
                                  RdKafka_VERSION VERSION_VAR RdKafka_VERSION)
mark_as_advanced(RdKafka_FOUND RdKafka_VERSION)

set(RdKafka_COMPONENTS RdKafka::rdkafka++ RdKafka::rdkafka)

if(RdKafka_FIND_COMPONENTS)
    foreach(_FIND_COMPONENT ${RdKafka_FIND_COMPONENTS})
        list(FIND RdKafka_COMPONENTS "RdKafka::${_FIND_COMPONENT}" _index)
        if(${_index} EQUAL -1)
            conan_message(FATAL_ERROR "Conan: Component '${_FIND_COMPONENT}' NOT found in package 'RdKafka'")
        else()
            conan_message(STATUS "Conan: Component '${_FIND_COMPONENT}' found in package 'RdKafka'")
        endif()
    endforeach()
endif()

########### VARIABLES #######################################################################
#############################################################################################


set(RdKafka_INCLUDE_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_INCLUDE_DIR "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_INCLUDES "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_RES_DIRS )
set(RdKafka_DEFINITIONS )
set(RdKafka_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)
set(RdKafka_COMPILE_DEFINITIONS )
set(RdKafka_COMPILE_OPTIONS_LIST "" "")
set(RdKafka_COMPILE_OPTIONS_C "")
set(RdKafka_COMPILE_OPTIONS_CXX "")
set(RdKafka_LIBRARIES_TARGETS "") # Will be filled later, if CMake 3
set(RdKafka_LIBRARIES "") # Will be filled later
set(RdKafka_LIBS "") # Same as RdKafka_LIBRARIES
set(RdKafka_SYSTEM_LIBS pthread rt dl m)
set(RdKafka_FRAMEWORK_DIRS )
set(RdKafka_FRAMEWORKS )
set(RdKafka_FRAMEWORKS_FOUND "") # Will be filled later
set(RdKafka_BUILD_MODULES_PATHS )

conan_find_apple_frameworks(RdKafka_FRAMEWORKS_FOUND "${RdKafka_FRAMEWORKS}" "${RdKafka_FRAMEWORK_DIRS}")

mark_as_advanced(RdKafka_INCLUDE_DIRS
                 RdKafka_INCLUDE_DIR
                 RdKafka_INCLUDES
                 RdKafka_DEFINITIONS
                 RdKafka_LINKER_FLAGS_LIST
                 RdKafka_COMPILE_DEFINITIONS
                 RdKafka_COMPILE_OPTIONS_LIST
                 RdKafka_LIBRARIES
                 RdKafka_LIBS
                 RdKafka_LIBRARIES_TARGETS)

# Find the real .lib/.a and add them to RdKafka_LIBS and RdKafka_LIBRARY_LIST
set(RdKafka_LIBRARY_LIST rdkafka++ rdkafka)
set(RdKafka_LIB_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/lib")

# Gather all the libraries that should be linked to the targets (do not touch existing variables):
set(_RdKafka_DEPENDENCIES "${RdKafka_FRAMEWORKS_FOUND} ${RdKafka_SYSTEM_LIBS} lz4::lz4;zstd::zstd;OpenSSL::OpenSSL;cyrus-sasl::cyrus-sasl")

conan_package_library_targets("${RdKafka_LIBRARY_LIST}"  # libraries
                              "${RdKafka_LIB_DIRS}"      # package_libdir
                              "${_RdKafka_DEPENDENCIES}"  # deps
                              RdKafka_LIBRARIES            # out_libraries
                              RdKafka_LIBRARIES_TARGETS    # out_libraries_targets
                              ""                          # build_type
                              "RdKafka")                                      # package_name

set(RdKafka_LIBS ${RdKafka_LIBRARIES})

foreach(_FRAMEWORK ${RdKafka_FRAMEWORKS_FOUND})
    list(APPEND RdKafka_LIBRARIES_TARGETS ${_FRAMEWORK})
    list(APPEND RdKafka_LIBRARIES ${_FRAMEWORK})
endforeach()

foreach(_SYSTEM_LIB ${RdKafka_SYSTEM_LIBS})
    list(APPEND RdKafka_LIBRARIES_TARGETS ${_SYSTEM_LIB})
    list(APPEND RdKafka_LIBRARIES ${_SYSTEM_LIB})
endforeach()

# We need to add our requirements too
set(RdKafka_LIBRARIES_TARGETS "${RdKafka_LIBRARIES_TARGETS};lz4::lz4;zstd::zstd;OpenSSL::OpenSSL;cyrus-sasl::cyrus-sasl")
set(RdKafka_LIBRARIES "${RdKafka_LIBRARIES};lz4::lz4;zstd::zstd;OpenSSL::OpenSSL;cyrus-sasl::cyrus-sasl")

set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH  ${CMAKE_PREFIX_PATH})


########### COMPONENT rdkafka VARIABLES #############################################

set(RdKafka_rdkafka_INCLUDE_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka_INCLUDE_DIR "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka_INCLUDES "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka_LIB_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/lib")
set(RdKafka_rdkafka_RES_DIRS )
set(RdKafka_rdkafka_DEFINITIONS )
set(RdKafka_rdkafka_COMPILE_DEFINITIONS )
set(RdKafka_rdkafka_COMPILE_OPTIONS_C "")
set(RdKafka_rdkafka_COMPILE_OPTIONS_CXX "")
set(RdKafka_rdkafka_LIBS rdkafka)
set(RdKafka_rdkafka_SYSTEM_LIBS pthread rt dl m)
set(RdKafka_rdkafka_FRAMEWORK_DIRS )
set(RdKafka_rdkafka_FRAMEWORKS )
set(RdKafka_rdkafka_BUILD_MODULES_PATHS )
set(RdKafka_rdkafka_DEPENDENCIES lz4::lz4 zstd::zstd OpenSSL::OpenSSL cyrus-sasl::cyrus-sasl)
set(RdKafka_rdkafka_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)

########### COMPONENT rdkafka++ VARIABLES #############################################

set(RdKafka_rdkafka++_INCLUDE_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka++_INCLUDE_DIR "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka++_INCLUDES "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/include")
set(RdKafka_rdkafka++_LIB_DIRS "/root/.conan/data/librdkafka/1.9.1/_/_/package/b4e80ecb92024849e458b8e81b2b429483432faf/lib")
set(RdKafka_rdkafka++_RES_DIRS )
set(RdKafka_rdkafka++_DEFINITIONS )
set(RdKafka_rdkafka++_COMPILE_DEFINITIONS )
set(RdKafka_rdkafka++_COMPILE_OPTIONS_C "")
set(RdKafka_rdkafka++_COMPILE_OPTIONS_CXX "")
set(RdKafka_rdkafka++_LIBS rdkafka++)
set(RdKafka_rdkafka++_SYSTEM_LIBS )
set(RdKafka_rdkafka++_FRAMEWORK_DIRS )
set(RdKafka_rdkafka++_FRAMEWORKS )
set(RdKafka_rdkafka++_BUILD_MODULES_PATHS )
set(RdKafka_rdkafka++_DEPENDENCIES RdKafka::rdkafka)
set(RdKafka_rdkafka++_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)


########## FIND PACKAGE DEPENDENCY ##########################################################
#############################################################################################

include(CMakeFindDependencyMacro)

if(NOT lz4_FOUND)
    find_dependency(lz4 REQUIRED)
else()
    conan_message(STATUS "Conan: Dependency lz4 already found")
endif()

if(NOT zstd_FOUND)
    find_dependency(zstd REQUIRED)
else()
    conan_message(STATUS "Conan: Dependency zstd already found")
endif()

if(NOT OpenSSL_FOUND)
    find_dependency(OpenSSL REQUIRED)
else()
    conan_message(STATUS "Conan: Dependency OpenSSL already found")
endif()

if(NOT cyrus-sasl_FOUND)
    find_dependency(cyrus-sasl REQUIRED)
else()
    conan_message(STATUS "Conan: Dependency cyrus-sasl already found")
endif()


########## FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #######################################
#############################################################################################

########## COMPONENT rdkafka FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############

set(RdKafka_rdkafka_FRAMEWORKS_FOUND "")
conan_find_apple_frameworks(RdKafka_rdkafka_FRAMEWORKS_FOUND "${RdKafka_rdkafka_FRAMEWORKS}" "${RdKafka_rdkafka_FRAMEWORK_DIRS}")

set(RdKafka_rdkafka_LIB_TARGETS "")
set(RdKafka_rdkafka_NOT_USED "")
set(RdKafka_rdkafka_LIBS_FRAMEWORKS_DEPS ${RdKafka_rdkafka_FRAMEWORKS_FOUND} ${RdKafka_rdkafka_SYSTEM_LIBS} ${RdKafka_rdkafka_DEPENDENCIES})
conan_package_library_targets("${RdKafka_rdkafka_LIBS}"
                              "${RdKafka_rdkafka_LIB_DIRS}"
                              "${RdKafka_rdkafka_LIBS_FRAMEWORKS_DEPS}"
                              RdKafka_rdkafka_NOT_USED
                              RdKafka_rdkafka_LIB_TARGETS
                              ""
                              "RdKafka_rdkafka")

set(RdKafka_rdkafka_LINK_LIBS ${RdKafka_rdkafka_LIB_TARGETS} ${RdKafka_rdkafka_LIBS_FRAMEWORKS_DEPS})

set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH  ${CMAKE_PREFIX_PATH})

########## COMPONENT rdkafka++ FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############

set(RdKafka_rdkafka++_FRAMEWORKS_FOUND "")
conan_find_apple_frameworks(RdKafka_rdkafka++_FRAMEWORKS_FOUND "${RdKafka_rdkafka++_FRAMEWORKS}" "${RdKafka_rdkafka++_FRAMEWORK_DIRS}")

set(RdKafka_rdkafka++_LIB_TARGETS "")
set(RdKafka_rdkafka++_NOT_USED "")
set(RdKafka_rdkafka++_LIBS_FRAMEWORKS_DEPS ${RdKafka_rdkafka++_FRAMEWORKS_FOUND} ${RdKafka_rdkafka++_SYSTEM_LIBS} ${RdKafka_rdkafka++_DEPENDENCIES})
conan_package_library_targets("${RdKafka_rdkafka++_LIBS}"
                              "${RdKafka_rdkafka++_LIB_DIRS}"
                              "${RdKafka_rdkafka++_LIBS_FRAMEWORKS_DEPS}"
                              RdKafka_rdkafka++_NOT_USED
                              RdKafka_rdkafka++_LIB_TARGETS
                              ""
                              "RdKafka_rdkafka++")

set(RdKafka_rdkafka++_LINK_LIBS ${RdKafka_rdkafka++_LIB_TARGETS} ${RdKafka_rdkafka++_LIBS_FRAMEWORKS_DEPS})

set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH  ${CMAKE_PREFIX_PATH})


########## TARGETS ##########################################################################
#############################################################################################

########## COMPONENT rdkafka TARGET #################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    # Target approach
    if(NOT TARGET RdKafka::rdkafka)
        add_library(RdKafka::rdkafka INTERFACE IMPORTED)
        set_target_properties(RdKafka::rdkafka PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                              "${RdKafka_rdkafka_INCLUDE_DIRS}")
        set_target_properties(RdKafka::rdkafka PROPERTIES INTERFACE_LINK_DIRECTORIES
                              "${RdKafka_rdkafka_LIB_DIRS}")
        set_target_properties(RdKafka::rdkafka PROPERTIES INTERFACE_LINK_LIBRARIES
                              "${RdKafka_rdkafka_LINK_LIBS};${RdKafka_rdkafka_LINKER_FLAGS_LIST}")
        set_target_properties(RdKafka::rdkafka PROPERTIES INTERFACE_COMPILE_DEFINITIONS
                              "${RdKafka_rdkafka_COMPILE_DEFINITIONS}")
        set_target_properties(RdKafka::rdkafka PROPERTIES INTERFACE_COMPILE_OPTIONS
                              "${RdKafka_rdkafka_COMPILE_OPTIONS_C};${RdKafka_rdkafka_COMPILE_OPTIONS_CXX}")
    endif()
endif()

########## COMPONENT rdkafka++ TARGET #################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    # Target approach
    if(NOT TARGET RdKafka::rdkafka++)
        add_library(RdKafka::rdkafka++ INTERFACE IMPORTED)
        set_target_properties(RdKafka::rdkafka++ PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                              "${RdKafka_rdkafka++_INCLUDE_DIRS}")
        set_target_properties(RdKafka::rdkafka++ PROPERTIES INTERFACE_LINK_DIRECTORIES
                              "${RdKafka_rdkafka++_LIB_DIRS}")
        set_target_properties(RdKafka::rdkafka++ PROPERTIES INTERFACE_LINK_LIBRARIES
                              "${RdKafka_rdkafka++_LINK_LIBS};${RdKafka_rdkafka++_LINKER_FLAGS_LIST}")
        set_target_properties(RdKafka::rdkafka++ PROPERTIES INTERFACE_COMPILE_DEFINITIONS
                              "${RdKafka_rdkafka++_COMPILE_DEFINITIONS}")
        set_target_properties(RdKafka::rdkafka++ PROPERTIES INTERFACE_COMPILE_OPTIONS
                              "${RdKafka_rdkafka++_COMPILE_OPTIONS_C};${RdKafka_rdkafka++_COMPILE_OPTIONS_CXX}")
    endif()
endif()

########## GLOBAL TARGET ####################################################################

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    if(NOT TARGET RdKafka::RdKafka)
        add_library(RdKafka::RdKafka INTERFACE IMPORTED)
    endif()
    set_property(TARGET RdKafka::RdKafka APPEND PROPERTY
                 INTERFACE_LINK_LIBRARIES "${RdKafka_COMPONENTS}")
endif()

########## BUILD MODULES ####################################################################
#############################################################################################
########## COMPONENT rdkafka BUILD MODULES ##########################################

foreach(_BUILD_MODULE_PATH ${RdKafka_rdkafka_BUILD_MODULES_PATHS})
    include(${_BUILD_MODULE_PATH})
endforeach()
########## COMPONENT rdkafka++ BUILD MODULES ##########################################

foreach(_BUILD_MODULE_PATH ${RdKafka_rdkafka++_BUILD_MODULES_PATHS})
    include(${_BUILD_MODULE_PATH})
endforeach()
